#include "Logger.hpp"

QFile Logger::file;
QTextStream Logger::out;

void Logger::initializeFile( QString filename )
{
#ifdef LOG_TO_FILE
    if( file.isOpen() ) {
        file.close();
    }
    file.setFileName( filename );
    if( file.open( QFile::WriteOnly | QFile::Text ) ) {
        out.setDevice( &file );
    }
#endif // #ifdef LOG_TO_FILE
}

void Logger::releaseFile()
{
#ifdef LOG_TO_FILE
    if( file.isOpen() ) {
       file.close();
    }
#endif // #ifdef LOG_TO_FILE
}

void Logger::write( QString message, Logger::LoggingMode mode )
{
#if defined(LOG_TO_FILE) || defined(LOG_TO_CONSOLE)
      QString output;
      switch( mode ) {
#ifdef LOG_NOTES
          case Logger::Note:
          output = "NOTE    : ";
          break;
#endif // #ifdef LOG_NOTES
#ifdef LOG_DEBUG
          case Logger::Debug:
          output = "DEBUG   : ";
          break;
#endif // #ifdef LOG_DEBUG
#ifdef LOG_WARNINGS
          case Logger::Warning:
          output = "WARNING : ";
          break;
#endif // #ifdef LOG_WARNINGS
#ifdef LOG_ERRORS
          case Logger::Error:
          output = "ERROR   : ";
          break;
#endif // #ifdef LOG_ERRORS
          default:
          return; // Do nothing
      }
      output += message;
#ifdef LOG_TO_CONSOLE
      if( mode == Logger::Error ) {
        cerr << output.toStdString() << endl;
      }
      else {
        cout << output.toStdString() << endl;
      }
#endif // #ifdef LOG_TO_CONSOLE
#ifdef LOG_TO_FILE
      out << output << endl;
#endif // #ifdef LOG_TO_FILE
#endif // #if defined(LOG_TO_FILE) || defined(LOG_TO_CONSOLE)
}

void Logger::write( QString className, QString functionName, QString msg, LoggingMode mode )
{
#if defined(LOG_TO_CONSOLE) || defined(LOG_TO_FILE)
    write( className + "::" + functionName + " -> " + msg, mode );
#endif // #if defined(LOG_TO_CONSOLE) || defined(LOG_TO_FILE)
}

Logger::Logger()
{

}
